/**
 * 
 */
package easyzhou.com.utils.svn;

import java.io.File;

import easyzhou.com.bean.SVNChangedLog;
import easyzhou.com.config.InitConfig;

/**
 *
 *
 * @author zhoucong
 * @date 2016年3月28日 上午11:15:58
 */
public class SVNLogHandler {
	private static final String PERFIX_MODIFIED = "Modified : /";
	private static final String PERFIX_ADDED = "Added : /";
	private static final String PERFIX_DELETED = "Deleted : /";
	private static final String PERFIX_M = "M /";
	private static final String PERFIX_A = "A /";
	private static final String PERFIX_D = "D /";

	// /**
	// * 解析项目名
	// *
	// * @param log
	// * 日志行
	// * @return
	// * @author zhoucong
	// * @date 2016年3月28日 上午11:13:17
	// */
	// public static String parseProductName(String log) {
	// String line = log.substring(1, log.length());
	// if (line.contains("/")) {
	// line = line.substring(0, line.indexOf("/"));
	// }
	// return line;
	// }

	/**
	 * 日志前缀
	 *
	 * @param log
	 * @param perfixModify
	 * @return
	 * @author zhoucong
	 * @date 2016年3月28日 上午11:13:37
	 */
	public static String parseLine(String log, String... perfixModify) {
		String line = log;
		for (String string : perfixModify) {
			line = line.replace(string, "");

		}
		if (line.contains("(")) {
			line = line.substring(0, line.indexOf("(")).trim();
		}

		return line;
	}

	public static boolean isDeleteLog(String log) {
		return log.startsWith(PERFIX_DELETED) || log.startsWith(PERFIX_D);
	}

	public static boolean isAddedLog(String log) {
		return log.startsWith(PERFIX_ADDED) || log.startsWith(PERFIX_A);
	}

	public static boolean isModifiedLog(String log) {
		return log.startsWith(PERFIX_MODIFIED) || log.startsWith(PERFIX_M);
	}

	/**
	 * 解析日志
	 *
	 * @param logs
	 * @return
	 * @author zhoucong
	 * @date 2016年3月28日 上午11:22:12
	 */
	public static SVNChangedLog parse(String logs) {
		String[] logList = logs.split("\\r?\\n");
		SVNChangedLog logContext = new SVNChangedLog();
		for (int i = logList.length - 1; i >= 0; i--) {
			String log = logList[i];
			if (log == null) {
				continue;
			}
			log = log.trim();
			if (SVNLogHandler.isModifiedLog(log)) {
				String line = SVNLogHandler.parseLine(log, PERFIX_MODIFIED, PERFIX_M);
				logContext.getModifiedSet().add(line);
				// logContext.setProductName(SVNLogHandler.parseProductName(line));
			}
			if (SVNLogHandler.isAddedLog(log)) {
				String line = SVNLogHandler.parseLine(log, PERFIX_ADDED, PERFIX_A);
				// logContext.setProductName(SVNLogHandler.parseProductName(line));
				logContext.getAddedSet().add(line);
			}
			if (SVNLogHandler.isDeleteLog(log)) {
				String line = SVNLogHandler.parseLine(log, PERFIX_DELETED, PERFIX_D);
				// logContext.setProductName(SVNLogHandler.parseProductName(line));
				logContext.getDeleteSet().add(line);
			}
		}
		return logContext;
	}

	/**
	 *
	 *
	 * @param filePath
	 *            文件路径
	 * @return
	 * @author zhoucong
	 * @date 2016年3月28日 下午2:06:54
	 */
	public static File[] doParsePath(File outputProjec, File localProject, String filePath) {
		String inputPath = SVNLogHandler.formatInputFilePath(localProject, filePath);

		File outputFile = null;
		outputFile = new File(outputProjec, SVNLogHandler.formatOutputFileName(filePath));

		return new File[] { new File(inputPath), outputFile };
	}

	public static String formatOutputFileName(String filePath) {
		filePath = filePath.replace(InitConfig.getPRIDUCT_FRIFIX(), "");
		filePath = deleteSourcePath(filePath);
		filePath = deleteOutputSourcePath(filePath);
		if (filePath.endsWith(".java")) {
			filePath = filePath.substring(0, filePath.length() - 5) + ".class";
		}

		if (filePath.endsWith(".class")) {
			return "/WEB-INF/classes/" + filePath;
		} else {
			return filePath.replace(InitConfig.getWEB_ROOT(), "");

		}
	}

	/**
	 *
	 * 去掉 Java 类的资源文件路径
	 * 
	 * @param filePath
	 * @return
	 * @author zhoucong
	 * @date 2016年3月28日 下午3:41:25
	 */
	private static String deleteOutputSourcePath(String filePath) {
		for (String sourcesFile : InitConfig.getPROPERTIES_SOURCES_FILES()) {
			int i = filePath.indexOf(sourcesFile);
			if (i >= 0) {
				filePath = "/WEB-INF/classes/" + filePath.substring(i + sourcesFile.length());
			}
		}
		return filePath;
	}

	/**
	 *
	 * 去掉 Java 类的资源文件路径
	 * 
	 * @param filePath
	 * @return
	 * @author zhoucong
	 * @date 2016年3月28日 下午3:41:25
	 */
	private static String deleteSourcePath(String filePath) {
		for (String sourcesFile : InitConfig.getJAVA_SOURCES_FILES()) {
			int i = filePath.indexOf(sourcesFile);
			if (i >= 0) {
				filePath = filePath.substring(i + sourcesFile.length());
			}
		}

		return filePath;
	}

	/**
	 *
	 *
	 * @param filePath
	 * @return
	 * @author zhoucong
	 * @date 2016年3月28日 下午2:00:02
	 */
	public static String formatInputFilePath(File localProject, String filePath) {
		filePath = filePath.replace(InitConfig.getPRIDUCT_FRIFIX(), "");
		filePath = deleteSourcePath(filePath);

		if (filePath.endsWith(".java")) {
			filePath = filePath.substring(0, filePath.length() - 5) + ".class";
		}

		if (filePath.endsWith(".class")) {

			File array[] = localProject.listFiles();
			String tmpPath = "";
			for (File subDir : array) {
				switch (subDir.getName()) {
				case "target":
					tmpPath = "/target/classes/" + filePath;
					filePath = localProject + tmpPath;
					break;
				case "WebContext":
					tmpPath = "/WebContext/classes/" + filePath;
					filePath = localProject + tmpPath;
					break;
				default:
					break;
				}
			}
		} else {
			filePath = localProject + "/" + filePath;
		}

		return filePath;
	}

}
